﻿using System.Windows.Controls;
using System.Windows.Media;
using System;
using System.Windows;
using Client.planet;
using System.Windows.Input;

namespace Client.star
{
    /// <summary>
    /// Interaction logic for Star.xaml
    /// </summary>
    public partial class Star : UserControl
    {
        public Star()
        {
            InitializeComponent();

            Background = Brushes.Black;

            this.IsMadeIsTrue += new EventHandler(Star_IsMadeIsTrue);
            this.LocationChanged += new EventHandler(Star_LocationChanged);
            this.IsShowingProductionChanged += new EventHandler(Star_IsShowingProductionChanged);            
        }

        void ShowStarName()
        {
            starName = new StarName();
            starName.BelongingStar = this;
            starName.label1.Content = StringStarName;
            starName.Measure(new Size(100D, 30D));
            double x = Location.X + image1.Width / 2 - starName.border1.DesiredSize.Width / 2;
            double y = Location.Y + image1.Height;
            LocationName = new Point(x, y);
            Canvas.SetLeft(starName, LocationName.X);
            Canvas.SetTop(starName, LocationName.Y);
            StartGameOptionsGlobals.StarMap.canvas1.Children.Add(starName);
        }
        void ShowStarProduction()
        {
            starProduction = new StarProduction();
            starProduction.label1.Content = StringStarProduction;
            starProduction.Measure(new Size(100D, 30D));
            double x = Location.X + image1.Width / 2 - starProduction.stackPanel1.DesiredSize.Width / 2;
            double y = Location.Y - starProduction.DesiredSize.Height;
            LocationProduction = new Point(x, y);
            Canvas.SetLeft(starProduction, LocationProduction.X);
            Canvas.SetTop(starProduction, LocationProduction.Y);
            StartGameOptionsGlobals.StarMap.canvas1.Children.Add(starProduction);
        }

        #region Events
        
        private void image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            
        }

        /// <summary>
        /// Display SolarSystem.
        /// </summary>
        private void UserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            SolarSystem solarSystem = new SolarSystem(PlanetArray);
            solarSystem.Title = StringStarName + " Solar System";
            e.Handled = true;
        }

        /// <summary>
        /// Show / Hide StarProduction
        /// </summary>
        void Star_IsShowingProductionChanged(object sender, EventArgs e)
        {
            if (IsShowingProduction)
                ShowStarProduction();
            else
                ((Canvas)starProduction.Parent).Children.Remove(starProduction);
        }
        /// <summary>
        /// Show / Hide StarName
        /// </summary>
        void Star_LocationChanged(object sender, EventArgs e)
        {
            if (IsShowingName)
            {
                ShowStarName();
            }
            if (IsShowingProduction)
            {
                ShowStarProduction();
            }
        }
        /// <summary>
        /// Initializes ToolTip
        /// </summary>
        void Star_IsMadeIsTrue(object sender, EventArgs e)
        {
            ToolTip tt = new ToolTip();
            ToolTipService.SetShowDuration(this, int.MaxValue);
            tt.Content = "Type: " + StarClass.StarType.ToString() + Environment.NewLine +
                "Subtype: " + Subtype.ToString() + Environment.NewLine +
                "Age: " + StarAge.ToString() + Environment.NewLine +
                "Luminosity Type: " + LuminosityClass.LuminosityType.ToString() + Environment.NewLine +
                "Luminosity Desc: " + LuminosityClass.LumStarDesc.ToString() + Environment.NewLine +
                "Mass: " + Mass.ToString() + Environment.NewLine +
                "Color: " + StarClass.StarColor.ToString() + Environment.NewLine +
                "Planets: " + PlanetArray.Length.ToString();
            ToolTip = tt;
        }
        #endregion

        #region Properties
        // Star attributes
        internal StarClass StarClass
        {
            get { return starClass; }
            set { starClass = value; }
        }
        internal LuminosityClass LuminosityClass
        {
            get { return luminosityClass; }
            set { luminosityClass = value; }
        }
        internal StarAge StarAge
        {
            get { return starAge; }
            set { starAge = value; }
        }
        public string StarImage
        {
            get { return starImage; }
            set { starImage = value; }
        }
        public double Mass
        {
            get { return mass; }
            set { mass = value; }
        }
        public int Subtype
        {
            get { return subtype; }
            set { subtype = value; }
        }
        public int Size
        {
            get { return size; }
            set
            {
                size = value;
                image1.Width = image1.Height = (double)size;
            }
        }
        public bool AtMainSeqLifePeriodEnd
        {
            get { return atMainSeqLifePeriodEnd; }
            set { atMainSeqLifePeriodEnd = value; }
        }
        public bool IsPostMainSeq
        {
            get { return isPostMainSeq; }
            set { isPostMainSeq = value; }
        }

        public Planet[] PlanetArray
        {
            get { return planetArray; }
            set { planetArray = value; }
        }

        public Point Location
        {
            get { return location; }
            set
            {
                location = value;
                LocationChanged(this, new EventArgs());
            }
        }
        public Point LocationName
        {
          get { return locationName; }
          set { locationName = value; }
        }
        public Point LocationProduction
        {
            get { return locationProduction; }
            set { locationProduction = value; }
        }
        public string StringStarName
        {
            get { return stringStarName; }
            set { stringStarName = value; }
        }
        public string StringStarProduction
        {
            get { return stringStarProduction; }
            set { stringStarProduction = value; }
        }
        public bool IsShowingName
        {
            get { return isShowingName; }
            set { isShowingName = value; }
        }
        public bool IsShowingProduction
        {
            get { return isShowingProduction; }
            set
            {
                isShowingProduction = value;
                IsShowingProductionChanged(this, new EventArgs());
            }
        }

        public ImageSource ImageSource
        {
            get { return imageSource; }
            set { image1.Source = imageSource = value; }
        }
        public bool IsMade
        {
            get { return isMade; }
            set
            {
                isMade = value;
                if (IsMadeIsTrue != null && isMade == true)
                    IsMadeIsTrue(this, new EventArgs());
            }
        }
        #endregion

        #region Fields
        // Star attributes
        StarClass starClass;
        LuminosityClass luminosityClass;
        StarAge starAge;
        string starImage;
        double mass;
        int subtype, size;
        bool atMainSeqLifePeriodEnd = false, isPostMainSeq = false;

        Planet[] planetArray;

        Point location, locationName, locationProduction;
        bool isShowingName = true, isShowingProduction = false;
        string stringStarName = "StarName", stringStarProduction = "StarProduction";
        StarName starName;
        StarProduction starProduction;

        ImageSource imageSource;
        public event EventHandler IsMadeIsTrue;
        public event EventHandler LocationChanged;
        public event EventHandler IsShowingProductionChanged;
        bool isMade = false;

        

        

        
        // If star is initialized by MakeStar and MakePlanets.
        #endregion
    }
}
